// pages/article/article.js
import CommunityModule from '../../models/3.0/community'
import { Login } from '../../models/login'
import dateDiff from '../../util/dateDiff'


Page({
  /**
   * Page initial data
   */
  data: {
    detail: {},
    id: '',
    commentList: [],
    replyFocus: false,
    placeholder: '我也来说两句~',
    newuswrID:'',
    followed:'',
    time:'',
    praiseNumber:'',
    commentNumber:'',
    transpondNumber:'',
    images:'',
    praised:'',
    coverUrl:'',
    newplaceholder:'',
    coment:'',
    newUserId:'',
    type:'',
    numb:'',
    transpondNumber:'',
    Collected:'',
  },

  /**
   * Lifecycle function--Called when page load
   */
  onLoad: function (options) {
    console.log(options );
    this.setData({
      time:options.date,
      collectionNumber:options.collectionNumber,
      praiseNumber:options.praiseNumber,
      commentNumber:options.commentNumber,
      transpondNumber:options.transpondNumber,
      images:options.images.split(','),
      coverUrl:options.coverUrl,
      id : options.id,
      type: options.type,
      collectionNumber:options.collectionNumber,
      newUserId: wx.getStorageSync('userInfo').user_id 
      })
    this.getMomentDetail(options.id)
    this.getComment()
  },

  getMomentDetail(id) {
    CommunityModule.momentDetail({
      id
    }).then((res) => {
        res.topicId=res?.topicId?.split(',')
        res.topicName = res?.topicName?.split(',')
      this.setData({
        Collected:res.collected,
        detail: res,
        numb:+res.momentsGroupId,
        followed:res.followed,
        newuswrID:wx.getStorageSync('userInfo').user_id,
        praised:res.praised
      })
      console.log('detail',res);
      console.log('detail',this.data.Collected);

  })
  },
  
  click(){
    if(this.data.followed===false){
      CommunityModule.guanzhu({
        fansUserId:this.data.detail.userId,
        userid:wx.getStorageSync('userInfo').user_id
      })
    }
    else{
      CommunityModule.noGuanzhu({
        fansUserId:this.data.detail.userId,
        userid:wx.getStorageSync('userInfo').user_id
      })
    }
    this.getMomentDetail(this.data.id)
  },


  // 动态下的评论数据
  getComment() {
    CommunityModule.momentComment({
      page: 1,
      count: 100,
      searchParam: this.data.id
    }).then((res) => {
      res.forEach((item) => {
        item.childComment &&
          item.childComment.forEach((childItem) => {
            childItem.date = dateDiff(+new Date(childItem.createDate))
          })
        item.date = dateDiff(+new Date(item.createDate))
      })
      this.setData({
        commentList: res
      })
    })
  },
 

  // 评论动态
  commnet(e) {
    this.setData({
      coment:e.detail
    })
    if(this.data.replyId?false:true ){
      CommunityModule.commnet({
        content: e.detail,
        momentId: this.data.id,
        type: 0,
        replyCommentId: this.data.replyId
      }).then((res) => {
        this.getComment()
      })
    }else{
      CommunityModule.commnet({
        content: e.detail,
        momentId: this.data.id,
        type: 1 ,
        replyCommentId: wx.getStorageSync('__ID')||this.data.replyId
      }).then((res) => {
        this.getComment()
      })
    }
  },

  onComonet() {
    this.setData({
      replyFocus: true
    })
  },

  reply(e) {
    this.setData({
      replyFocus: true,
      replyId: e.detail.id,
      placeholder:  '回答 ' +e.detail.name,
      newplaceholder: '回复 ' +e.detail.newName
    })
  }, 

  blur() {
    this.setData({
      replyFocus: false,
      replyId: null,
      placeholder: '我也来说两句~'
    })
  },

  // 点击喜欢还是不喜欢
  like() {
    if(this.data.praised===false){
    CommunityModule.momentLike({
      id: this.data.id,
      type: 0,
    }).then(res=>{
      this.setData({
        praised:true,
        praiseNumber:+(this.data.praiseNumber)+1
      })
    })
  } else{
    CommunityModule.noMomentLike({
      id: this.data.id,
      type: 0
    }).then(res=>{
      this.setData({
        praised:false,
        praiseNumber:+(this.data.praiseNumber)-1
      })
    })
  } 
  },
  // 点击是是否收藏
  star() {
    if(this.data.Collected===false){
    CommunityModule.cancelCollection({
      momentId: this.data.id,
    }).then(res=>{
      this.setData({
        Collected:true,
        collectionNumber:+(this.data.collectionNumber)+1
      })
    })
  } else{
    CommunityModule.nocancelCollection({
      momentId: this.data.id,
    }).then(res=>{
      this.setData({
        Collected:false,
        collectionNumber:+(this.data.collectionNumber)-1
      })
    })
  } 
  },
})
